<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}菜单`">
    <el-form
      ref="ruleFormRef"
      label-suffix=":"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="路由路径(path)" prop="path">
        <el-input v-model="drawerProps.row!.path" placeholder="请填写路由路径" clearable></el-input>
      </el-form-item>
      <el-form-item label="栏目名称(title)" prop="title">
        <el-input v-model="drawerProps.row!.title" placeholder="请填写栏目名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="路由名称(name)" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写路由名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="子级组件路径(component)" prop="component">
        <el-input v-model="drawerProps.row!.component" placeholder="子级请填写组件路径" clearable></el-input>
      </el-form-item>
      <el-form-item label="父级重定向(redirect)" prop="redirect">
        <el-input v-model="drawerProps.row!.redirect" placeholder="父级请填写重定向子路由" clearable></el-input>
      </el-form-item>
      <el-form-item label="图标(icon)" prop="icon">
        <SelectIcon v-model:icon-value="drawerProps.row!.icon" />
      </el-form-item>
      <el-form-item label="高亮菜单路径(activeMenu)" prop="activeMenu">
        <el-input v-model="drawerProps.row!.activeMenu" placeholder="详情页时，需要高亮的外部菜单" clearable></el-input>
      </el-form-item>
      <el-form-item label="外链(isLink)" prop="isLink">
        <el-input v-model="drawerProps.row!.isLink" placeholder="外部链接" clearable></el-input>
      </el-form-item>
      <el-form-item label="缓存页面(isKeepAlive)" prop="isKeepAlive">
        <el-switch
          v-model="drawerProps.row!.isKeepAlive"
          :active-value="1"
          :inactive-value="0"
          active-text="是"
          inactive-text="否"
        />
      </el-form-item>
      <el-form-item label="隐藏(isHide)" prop="isHide">
        <el-switch v-model="drawerProps.row!.isHide" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" />
      </el-form-item>
      <el-form-item label="全屏(isFull)" prop="isFull">
        <el-switch v-model="drawerProps.row!.isFull" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" />
      </el-form-item>
      <el-form-item label="固定在标签页(isAffix)" prop="isAffix">
        <el-switch v-model="drawerProps.row!.isAffix" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">{{ `${drawerProps.title}确定` }}</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="MenuDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import SelectIcon from "@/components/SelectIcon/index.vue";

const rules = reactive({
  path: [{ required: true, message: "请填写路由路径" }],
  title: [{ required: true, message: "请选择栏目名称" }],
  // component: [{ required: true, message: "请填写组件路径" }],
  name: [{ required: true, message: "请填写路由名称" }]
  // icon: [{ required: true, message: "请选择图标" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  console.log(params);
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    console.log(drawerProps.value.row);
    // try {
    //   await drawerProps.value.api!(drawerProps.value.row);
    //   ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
    //   drawerProps.value.getTableList!();
    //   drawerVisible.value = false;
    // } catch (error) {
    //   console.log(error);
    // }
  });
};

defineExpose({
  acceptParams
});
</script>
